#include "Game.h"
#include "GameStates.h"
#include "DXEngine.h"
#include "DXMesh.h"
#include "DXCamera.h"
#include <iostream>
using namespace std;



CGame::CGame() : m_bRunning(false),
				m_pCurrentState(0)
{
	m_pCurrentState = CGameStateMainMenu::getInstance();
}

CGame::~CGame()
{
	m_pScene.reset();
	CDXEngine::getInstance()->shutdown();
}


void CGame::init()
{
	unsigned int uiScreenWidth = 800;
	unsigned int uiScreenHeight = 600;

	m_bRunning = CDXEngine::getInstance()->init("Sphere Hunter", uiScreenWidth, uiScreenHeight, true);
	m_pScene.reset(new CSceneGraph());

	DXMeshPtr newMesh = DXMeshPtr(new CDXMesh());
	newMesh->load("Cube.x");
	m_pScene->addMesh(newMesh);
	ObjectPtr newObj = ObjectPtr(new CObject());
	newObj->setMeshID(newMesh->getID());
	m_pScene->addObject(newObj);
	DXCameraPtr newCam = DXCameraPtr(new CDXCamera());
	newCam->setView(t_view(	Vector4(0.0f, -10.0f, 0.0f),	// pos
							Vector4(1.0f, 0.0f, 0.0f),		// right
							Vector4(0.0f, 1.0f, 0.0f),		// up
							Vector4(0.0f, 0.0f, -1.0f)));	// look
	newCam->setPerspective(t_persp(	float(M_PI) /4.0f,		// FoV	
									((float)uiScreenWidth) / ((float)uiScreenHeight), // Aspct Ratio
									1.0f,					// Near
									1000.0f));				// Far
	newCam->setViewPort(t_viewPort(0,						// X
									0,						// Y
									uiScreenWidth,			// Screen width
									uiScreenHeight,			// Screen Height
									0.0f,					// minZ
									1.0f));					// maxZ
	m_pScene->addCamera(newCam);
}


void CGame::run()
{
	while (m_bRunning)
	{
		update();
	}
}

void CGame::update()
{
	m_bRunning = CDXEngine::getInstance()->update();
	if (m_pCurrentState)
	{
		m_pCurrentState->update(this);
	}
	CDXEngine::getInstance()->render(m_pScene);
}


void CGame::changeState(IGameState* pNewState)
{
	if (m_pCurrentState && pNewState)
	{
		m_pCurrentState->exit(this);
		m_pCurrentState = pNewState;
		m_pCurrentState->enter(this);
	}
}